package ru.spb.nicetu.schoolproject.gui;

import ru.spb.nicetu.schoolproject.MyToolBar;
import ru.spb.nicetu.schoolproject.data.HibernateTblModel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.CookieHandler;

/**
 * Created by dchernyshov on 09.04.15.
 */
public class CommonDialog<T extends HibernateTblModel> extends JDialog {
    protected JTable table;
    protected T tblModel;
    protected Box lastBox;
    protected JLabel labelForTableTitle;

    protected JButton btnPrevious;
    protected JButton btnNext;

    public CommonDialog() {}

    public CommonDialog(T tblModel) {
        this.tblModel = tblModel;
        setFrameSize();
        setModalityType(ModalityType.APPLICATION_MODAL);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        Box vbox = createVerticalBox();
        JToolBar myToolBar = new MyToolBar(tblModel.getClass().getName(), tblModel, table);
        add(myToolBar, BorderLayout.NORTH);
        add(vbox, BorderLayout.CENTER);
    }

    public void setFrameSize() {
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        int screenHeight = screenSize.height;
        int screenWidth = screenSize.width;
        setSize(screenWidth / 4, screenHeight / 2);
        setLocationByPlatform(true);
    }

    public Box createLabelBox() {
        Box box = Box.createHorizontalBox();
        labelForTableTitle = new JLabel();
        box.add(labelForTableTitle);
        box.add(Box.createHorizontalGlue());
        return box;
    }

    public Box createFirstHorizontalBox() {
        Box box = Box.createHorizontalBox();
        table = new JTable(tblModel);
        JScrollPane scrollPane = new JScrollPane(table);
        table.setMaximumSize(table.getMaximumSize());
        box.add(scrollPane);
        return box;
    }

    public Box createSecondHorizontalBox() {
        Box box = Box.createHorizontalBox();
        //btnPrevious = new JButton(createImageIcon("../../../../images/Back.png"));
        btnPrevious = new JButton("Назад");
        btnPrevious.addActionListener(new BtnPreviousListener());
        box.add(btnPrevious);
        //btnNext = new JButton(createImageIcon("../../../../images/Forward.png"));
        btnNext = new JButton("Вперед");
        btnNext.addActionListener(new BtnNextListener());
        box.add(Box.createHorizontalStrut(20));
        box.add(btnNext);
        return box;
    }

    public Box createVerticalBox() {
        Box box = Box.createVerticalBox();
        Box labelBox = createLabelBox();
        box.add(labelBox);
        box.add(Box.createVerticalStrut(10));
        Box hbox1 = createFirstHorizontalBox();
        box.add(hbox1);
        box.add(Box.createHorizontalStrut(10));
        lastBox = createSecondHorizontalBox();
        box.add(lastBox);
        return box;
    }


    public class BtnNextListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            tblModel.nextPage();
            tblModel.fireTableDataChanged();
        }
    }

    public class BtnPreviousListener implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            tblModel.previousPage();
            tblModel.fireTableDataChanged();
        }
    }
}
